package com.woniuxy.cloud.ticket52.bossassembly.apiimpl;


import com.google.common.collect.ImmutableMap;
import com.woniuxy.cloud.ticket52.BossMovieManageAPI;
import com.woniuxy.cloud.ticket52.MovieAPI;
import com.woniuxy.cloud.ticket52.bossassembly.common.JWTHelper;
import com.woniuxy.cloud.ticket52.bossassembly.model.Oplog;
import com.woniuxy.cloud.ticket52.bossassembly.service.IOplogService;
import com.woniuxy.cloud.ticket52.common.result.Result;
import com.woniuxy.cloud.ticket52.form.BossMovieAddForm;
import com.woniuxy.cloud.ticket52.form.MovieAddForm;
import io.jsonwebtoken.JwtHandler;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 操作日志 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2021-04-27
 */
@RestController
@Slf4j
public class BossMovieManageApiImpl implements BossMovieManageAPI {

    @Autowired
    private IOplogService oplogService;

    @Autowired
    private MovieAPI movieAPI;

    @Override
    @GlobalTransactional
    public Result addMovie(BossMovieAddForm form) {
        log.info("进入boss新增电影操作 {}",form);

        //调用底层的movie服务，新增电影
        //透传，外部传入的数据，直接传给底层服务
        MovieAddForm m = new MovieAddForm();
        BeanUtils.copyProperties(form,m);
        //本地生成
        m.setOrigin("BOSS");
        movieAPI.addMovie(m);


        //记录操作日志(本地操作）
        Oplog opLog = new Oplog();
        opLog.setOpName("MOVIE_ADD");
        opLog.setOpTime(new Date());
        //从header中获取网关透传的operatorId
//        HttpServletRequest request = ((ServletRequestAttributes) (RequestContextHolder.currentRequestAttributes())).getRequest();
//
//        int operatorId = request.getIntHeader("operatorId");
//        String operatorName = request.getHeader("operatorName");
//        log.info("从header中获取到的数据：{},{}",operatorId,operatorName);
        opLog.setOperatorId(123);
        int x = 1/0;
        opLog.setOperatorName("operatorName");
        oplogService.save(opLog);


        return Result.success();
    }
    @Autowired
    private JWTHelper jwtHelper;

    @Override
    public Result login(String operatorName, String password) {
        log.info("操作员登录：{}",operatorName);
        Date date = DateUtils.addDays(new Date(), 1);

        Map<String,Object> payload  = new HashMap<>();


        payload.put("operatorId",10086);
        payload.put("operatorName","maoshiba");

        String jwt = jwtHelper.createJWT(date, payload);

        return Result.success(jwt);
    }
}

